"""
Case Type   : WDR
Case Name   : 开启数据库监控快照功能,验证超过快照数量最大值是否清理（默认参数）
Create At   : 2023.1.13
Owner       : @haomeng802
Description :
    1、修改参数，开启快照功能
    2、手动生成快照,循环200次,查看snapshot_id最小的快照数据是否被清理(最大数量通过计算为192)
    3、使用默认系统库，查看快照数据
    4、清理环境，恢复参数
Expect      :
    1、开启快照功能成功
    2、循环手动生成快照数据成功
    3、查看快照数据成功，超过快照数量最大值后，snapshot_id较小的数据被清理
    4、清理环境成功，恢复参数成功
History     :
"""

import os
import time
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class WDR(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.sh_pri = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.config_wdr = "enable_wdr_snapshot"
        self.table = 'snapshot.snapshot'

    def test_wdr(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '-----step1:检查参数，修改参数配置;expect:成功-----'
        self.log.info(text)

        check_wdr = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(check_wdr)
        if 'on' != check_wdr.splitlines()[-2].strip():
            self.sh_pri.execute_gsguc('reload',
                                      self.constant.GSGUC_SUCCESS_MSG,
                                      f'''{self.config_wdr} = "on"''')

        param = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.assertEqual('on', param.splitlines()[-2].strip(),
                         '执行失败:' + text)

        text = '-----step2:手动生成快照,循环200次;expect:生成快照数据成功-----'
        self.log.info(text)

        for i in range(200):
            select_cmd = f'''select create_wdr_snapshot();'''
            self.log.info(f'---这是第{i}次创建快照---')
            res = self.sh_pri.execut_db_sql(select_cmd, dbname='postgres')
            self.log.info(res)
            self.assertEqual('WDR snapshot request has been submitted',
                             res.splitlines()[-2].strip(),
                             '执行失败:' + text)
            time.sleep(6)

        text = '-----step3:使用默认系统库，查看快照数据;' \
               'expect:查看快照数据成功，超过快照数量最大值后，snapshot_id较小的数据被清理-----'
        self.log.info(text)

        create_snap = f'''select * from {self.table};
            select count(*) from {self.table};'''
        self.log.info(create_snap)
        create_res = self.sh_pri.execut_db_sql(create_snap, dbname='postgres')
        self.log.info(create_res)
        self.assertEqual('192', create_res.splitlines()[-2].strip(),
                         '执行失败:' + text)

    def tearDown(self):
        text = '-----step4:清理环境,还原参数;expect:成功-----'
        self.log.info(text)

        drop_snapshot = f'''truncate table {self.table};
            select count(*) from {self.table};'''
        self.log.info(drop_snapshot)
        drop_res = self.sh_pri.execut_db_sql(drop_snapshot, dbname='postgres')
        self.log.info(drop_res)

        recov_wdr = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(recov_wdr)
        if 'off' != recov_wdr.splitlines()[-2].strip():
            self.sh_pri.execute_gsguc('reload',
                                      self.constant.GSGUC_SUCCESS_MSG,
                                      f'''{self.config_wdr} = "off"''')

        param1 = self.sh_pri.execut_db_sql(f'''show {self.config_wdr};''')
        self.log.info(param1)

        self.assertEqual('off', param1.splitlines()[-2].strip(), '参数恢复失败')
        self.assertEqual('0', drop_res.splitlines()[-2].strip(),
                         '执行失败:' + text)
        self.log.info(f'----{os.path.basename(__file__)} end-----')
